/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */


var removeNthFromEnd = function (head, n) {
  let arr = []
  let p = head
  while (p) {
    arr.unshift(p.val)
    p = p.next
  }

  arr.splice(n, 1)
  let dummy = new ListNode();
  for (let i = arr.length - 1; i >= 0; i--) {
    dummy.val = arr[i]
    dummy = dummy.next
  }

  return dummy
}
